home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
prolog
/
ai.prl
/
opnprlg1.hqx
/
Open Prolog
/
External Predicates…
/
Sources
/
prlxLibraries.p
< prev
next >
Wrap
Text File
|
1993-04-15
|
15KB
|
504 lines
{$D+} { MacsBug symbols on }
{$R-} { No range checking }
UNIT prlxLibraries;
INTERFACE
USES memtypes, quickdraw, osintf, toolintf, packintf, prlxdefinitions;
PROCEDURE writestr(st: str255; plist: prlxPtr);
PROCEDURE writelnstr(st: str255; plist: prlxPtr);
PROCEDURE errorstr(st: str255; plist: prlxPtr);
FUNCTION returnValue(termNumber: termIndex; n: longint;
plist: prlxPtr): boolean;
FUNCTION returnStructure(termNumber: termIndex; st: str255; arity: integer;
plist: prlxPtr): boolean;
FUNCTION returnList(termNumber: termIndex; plist: prlxPtr): boolean;
FUNCTION returnAtom(termNumber: termIndex; st: str255;
plist: prlxPtr): boolean;
FUNCTION returnUnifiedTerms(a, b: termIndex; plist: prlxPtr): boolean;
FUNCTION subterm(subtermordinate: integer; termNumber: termIndex;
plist: prlxPtr): termIndex;
FUNCTION newFreeTerm(plist: prlxPtr): termIndex;
FUNCTION number(termNumber: termIndex; plist: prlxPtr): boolean;
FUNCTION atom(termNumber: termIndex; plist: prlxPtr): boolean;
FUNCTION structure(termNumber: termIndex; plist: prlxPtr): boolean;
FUNCTION list(termNumber: termIndex; plist: prlxPtr): boolean;
FUNCTION variable(termNumber: termIndex; plist: prlxPtr): boolean;
FUNCTION value(termNumber: termIndex; plist: prlxPtr): longint;
FUNCTION arity(termNumber: termIndex; plist: prlxPtr): integer;
FUNCTION text(termNumber: termIndex; plist: prlxPtr): str255;
FUNCTION drawAlert(ALRTid: integer; st: str255; plist: prlxPtr): longint;
FUNCTION centreDialog(DLOGid: integer; plist: prlxPtr): longint;
PROCEDURE centreSfGetTEXTFile(vertical: integer; str: str255;
VAR reply: sfReply);
PROCEDURE centreSfPutFile(vertical: integer; str: str255; origName: str255;
dlgHook: procPtr; VAR reply: sfReply);
FUNCTION getFileName(VAR FileName: str255;
VAR FileVolume: longint): boolean;
FUNCTION predicateNameAndArity(VAR name: str255; VAR arity: integer;
plist: prlxPtr): boolean;
PROCEDURE signalError(error: integer; argumentIndex: termIndex;
hostErrorCode: longint; errorMessage: str255;
plist: prlxPtr);
IMPLEMENTATION
PROCEDURE signalError(error: integer; argumentIndex: termIndex;
hostErrorCode: longint; errorMessage: str255;
plist: prlxPtr);
{if you want to throw an error from an external predicate, use this}
{error kind is an index to an ISO error type - see prlxDefinitions.p}
{hostErrorCode is where you can put a mac error code}
VAR
i: integer;
t, r, q: termIndex;
ignoreBoolean: boolean;
thePredicateName: str255;
thePredicateArity: integer;
BEGIN
ignoreBoolean := predicateNameAndArity(thePredicateName,
thePredicateArity, plist);
t := newFreeTerm(plist);
ignoreBoolean := returnList(t, plist); {return a list of error
information}
q := subterm(1, t, plist);
ignoreBoolean := returnStructure(q, 'goal', 1, plist); {first, the goal
- functor & arguments}
q := subterm(1, q, plist);
ignoreBoolean := returnStructure(q, thePredicateName, thePredicateArity,
plist);
FOR i := 1 TO thePredicateArity DO
ignoreBoolean := returnUnifiedTerms(subterm(i, q, plist), i, plist);{the
goal's arguments}
q := t;
IF argumentIndex <> 0 {if the argument index = 0, no argument index info
returned}
THEN
BEGIN
q := subterm(2, q, plist);
ignoreBoolean := returnList(q, plist);
r := subterm(1, q, plist);
ignoreBoolean := returnStructure(r, 'argument_index', 1, plist);
r := subterm(1, r, plist);
ignoreBoolean := returnValue(r, argumentIndex, plist);
END;
IF hostErrorCode <> 0 {if the mac error code = 0, no host error info
returned}
THEN
BEGIN
q := subterm(2, q, plist);
ignoreBoolean := returnList(q, plist);
r := subterm(1, q, plist);
ignoreBoolean := returnStructure(r, 'host_error_code', 1, plist);
r := subterm(1, r, plist);
ignoreBoolean := returnValue(r, hostErrorCode, plist);
END;
IF errorMessage <> '' {only return an error message term if it's
non-blank}
THEN
BEGIN
q := subterm(2, q, plist);
ignoreBoolean := returnList(q, plist);
r := subterm(1, q, plist);
ignoreBoolean := returnStructure(r, 'error_message', 1, plist);
r := subterm(1, r, plist);
ignoreBoolean := returnAtom(r, errorMessage, plist);
END;
ignoreBoolean := returnAtom(subterm(2, q, plist), '[]', plist);
WITH plist^ DO
BEGIN
outcome := error; {outcome is normally 'notAnErrorCode' - this puts a
real error code there}
data[1] := t;
END;
END;
PROCEDURE writestr(st: str255; plist: prlxPtr);
BEGIN
WITH plist^ DO
BEGIN
callbackrequest := writestring;
s := st;
callback(entrypoint);
END;
END;
PROCEDURE writelnstr(st: str255; plist: prlxPtr);
BEGIN
WITH plist^ DO
BEGIN
callbackrequest := writelnstring;
s := st;
callback(entrypoint);
END;
END;
PROCEDURE errorstr(st: str255; plist: prlxPtr);
BEGIN
WITH plist^ DO
BEGIN
callbackrequest := writeerror;
s := st;
callback(entrypoint);
END;
END;
FUNCTION predicateNameAndArity(VAR name: str255; VAR arity: integer;
plist: prlxPtr): boolean;
BEGIN
WITH plist^ DO
BEGIN
callbackrequest := getPredicateNameAndArity;
callback(entrypoint);
predicateNameAndArity := callbackData[3] = messageOK;
name := s;
arity := callbackData[1];
END;
END;
FUNCTION returnUnifiedTerms(a, b: termIndex; plist: prlxPtr): boolean;
BEGIN
WITH plist^ DO
BEGIN
callbackrequest := unifyTerms;
callbackData[1] := a;
callbackData[2] := b;
callback(entrypoint);
returnUnifiedTerms := callbackData[3] = messageOK;
END;
END;
FUNCTION returnValue(termNumber: termIndex; n: longint;
plist: prlxPtr): boolean;
BEGIN
WITH plist^ DO
BEGIN
callbackrequest := unifyToInteger;
callbackData[1] := termNumber;
callbackData[2] := n;
callback(entrypoint);
returnValue := callbackData[3] = messageOK;
END;
END;
FUNCTION returnList(termNumber: termIndex; plist: prlxPtr): boolean;
BEGIN
WITH plist^ DO
BEGIN
callbackrequest := unifyToFunctor;
callbackData[1] := termNumber;
callbackData[3] := 2;
s := '.';
callback(entrypoint);
returnList := callbackData[3] = messageOK;
END;
END;
FUNCTION returnStructure(termNumber: termIndex; st: str255; arity: integer;
plist: prlxPtr): boolean;
BEGIN
WITH plist^ DO
BEGIN
callbackrequest := unifyToFunctor;
callbackData[1] := termNumber;
callbackData[3] := arity;
s := st;
callback(entrypoint);
returnStructure := callbackData[3] = messageOK;
END;
END;
FUNCTION returnAtom(termNumber: termIndex; st: str255;
plist: prlxPtr): boolean;
BEGIN
returnAtom := returnStructure(termNumber, st, 0, plist);
END;
FUNCTION subterm(subtermordinate: integer; termNumber: termIndex;
plist: prlxPtr): termIndex;
BEGIN
WITH plist^ DO
BEGIN
callbackrequest := getsubterm;
callbackData[1] := termNumber;
callbackData[2] := subtermordinate;
callback(entrypoint);
subterm := callbackData[3];
END;
END;
FUNCTION newFreeTerm(plist: prlxPtr): termIndex;
BEGIN
WITH plist^ DO
BEGIN
callbackrequest := getFreeTerm;
callback(entrypoint);
newFreeTerm := callbackData[1];
END;
END;
FUNCTION number(termNumber: termIndex; plist: prlxPtr): boolean;
BEGIN
WITH plist^ DO
BEGIN
callbackrequest := getterminfo;
callbackData[1] := termNumber;
callback(entrypoint);
number := (callbackData[1] = integertag);
END;
END;
FUNCTION atom(termNumber: termIndex; plist: prlxPtr): boolean;
BEGIN
WITH plist^ DO
BEGIN
callbackrequest := getterminfo;
callbackData[1] := termNumber;
callback(entrypoint);
atom := (callbackData[1] = atomtag);
END;
END;
FUNCTION structure(termNumber: termIndex; plist: prlxPtr): boolean;
BEGIN
WITH plist^ DO
BEGIN
callbackrequest := getterminfo;
callbackData[1] := termNumber;
callback(entrypoint);
structure := (callbackData[1] = structuretag);
END;
END;
FUNCTION list(termNumber: termIndex; plist: prlxPtr): boolean;
BEGIN
WITH plist^ DO
BEGIN
callbackrequest := getterminfo;
callbackData[1] := termNumber;
callback(entrypoint);
list := ((callbackData[1] = structuretag) AND (s = '.') AND
(callbackData[2] = 2)) OR ((callbackData[1] = atomtag) AND
(s = '[]'));
END;
END;
FUNCTION variable(termNumber: termIndex; plist: prlxPtr): boolean;
BEGIN
WITH plist^ DO
BEGIN
callbackrequest := getterminfo;
callbackData[1] := termNumber;
callback(entrypoint);
variable := (callbackData[1] = variabletag);
END;
END;
FUNCTION value(termNumber: termIndex; plist: prlxPtr): longint;
BEGIN
WITH plist^ DO
BEGIN
callbackrequest := getterminfo;
callbackData[1] := termNumber;
callback(entrypoint);
IF callbackData[1] = integertag
THEN value := callbackData[2]
ELSE errorstr('attempt to get value of a non-integer', plist);
END;
END;
FUNCTION arity(termNumber: termIndex; plist: prlxPtr): integer;
BEGIN
WITH plist^ DO
BEGIN
callbackrequest := getterminfo;
callbackData[1] := termNumber;
callback(entrypoint);
CASE callbackData[1] OF
atomtag, integertag, variabletag: arity := 0;
structuretag: arity := callbackData[2];
OTHERWISE errorstr('Funny data from getTermInfo in arity', plist);
END;
END;
END;
FUNCTION text(termNumber: termIndex; plist: prlxPtr): str255;
VAR
st: str255;
i: integer;
BEGIN
WITH plist^ DO
BEGIN
callbackrequest := getterminfo;
callbackData[1] := termNumber;
callback(entrypoint);
CASE callbackData[1] OF
atomtag, structuretag: text := s;
integertag:
BEGIN
numtostring(callbackData[2], st);
text := st;
END;
variabletag:
BEGIN
numtostring(callbackData[2], st);
FOR i := 255 DOWNTO 2 DO st[i] := st[i - 1];
st[1] := '_';
text := st;
END;
OTHERWISE errorstr('Funny data from getTermInfo in text', plist);
END;
END;
END;
FUNCTION drawAlert(ALRTid: integer; st: str255; plist: prlxPtr): longint;
BEGIN
WITH plist^ DO
BEGIN
callbackrequest := drawALRT;
callbackData[1] := ALRTid;
s := st;
callback(entrypoint);
drawAlert := callbackData[2];
END;
END;
FUNCTION centreDialog(DLOGid: integer; plist: prlxPtr): longint;
VAR
item: integer;
myDialog: dialogPtr;
BEGIN
WITH plist^ DO
BEGIN
(* ###hack callbackrequest := drawDLOG;
callbackData[1] := DLOGid;
callback(entrypoint);
centreDialog := callbackData[2]; *)
myDialog := getNewDialog(DLOGid, NIL, windowPtr(1));
showWindow(myDialog);
modalDialog(NIL, item);
disposDialog(myDialog);
centreDialog := item;
END;
END;
PROCEDURE centreSfGetTEXTFile(vertical: integer; str: str255;
VAR reply: sfReply);
VAR
myPoint: point;
dialogHandle: dialogTHndl;
myPort: grafPtr;
screenWidth, dialogWidth: integer;
myTypeList: sfTypeList;
BEGIN
myTypeList[0] := 'TEXT';
getPort(myPort);
WITH myPort^.portBits.bounds DO screenWidth := right - left;
dialogHandle := dialogTHndl(getResource('DLOG', getDlgId));
WITH dialogHandle^^.boundsRect DO
BEGIN
dialogWidth := right - left;
myPoint.h := (screenWidth - dialogWidth) DIV 2;
myPoint.v := vertical;
END;
sfGetFile(myPoint, str, NIL, 1, myTypeList, NIL, reply);
END;
PROCEDURE centreSfPutFile(vertical: integer; str: str255; origName: str255;
dlgHook: procPtr; VAR reply: sfReply);
VAR
myPoint: point;
dialogHandle: dialogTHndl;
myPort: grafPtr;
screenWidth, dialogWidth: integer;
BEGIN
getPort(myPort);
WITH myPort^.portBits.bounds DO screenWidth := right - left;
dialogHandle := dialogTHndl(getResource('DLOG', putDlgId));
WITH dialogHandle^^.boundsRect DO
BEGIN
dialogWidth := right - left;
myPoint.h := (screenWidth - dialogWidth) DIV 2;
myPoint.v := vertical;
END;
sfPutFile(myPoint, str, origName, dlgHook, reply);
END;
FUNCTION getFileName(VAR FileName: str255;
VAR FileVolume: longint): boolean;
VAR
reply: sfReply;
BEGIN
centreSfGetTEXTFile(75, '', reply);
FileName := reply.fName;
FileVolume := reply.vRefNum;
getFileName := reply.good;
END;
END.